<template>
  <div>
    <el-dialog
      :title="info.isadd?'添加':'修改'"
      :visible.sync="info.isshow"
      @closed="closed"
      @opened="opened"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="规格名称">
          <el-input v-model="form.specsname"></el-input>
        </el-form-item>
        <el-form-item label="规格属性" v-for="(item,index) in arr" :key="index">
          <div class="specsbtn">
            <el-input v-model="item.value"></el-input>
            <el-button type="primary" v-if="index==0" @click="addArr">新增规格属性</el-button>
            <el-button type="danger" v-else @click="del(index)">删除</el-button>
          </div>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add">{{info.isadd?'添加':'修改'}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { reqspecsadd, reqspecsdit } from "../../../request/api.js";
export default {
  data() {
    return {
      form: {
        specsname: "",
        attrs: "",
        status: 1
      },
      arr: [
        {
          value: ""
        }
      ]
    };
  },
  computed: {
    ...mapGetters({
      info: "specs/info",
      specsinfo: "specs/specsinfo"
    })
  },
  methods: {
    ...mapActions({
      reqspecslist: "specs/specslist"
    }),
    cancel() {
      this.info.isshow = false;
    },
    add() {
      this.form.attrs = this.arr.map(item => item.value).join(",");
      if (this.info.isadd) {
        reqspecsadd(this.form).then(res => {
          if (res.data.code == 200) {
            this.reqspecslist();
            this.info.isshow = false;
          }
        });
      } else {
        reqspecsdit(this.form).then(res => {
          if (res.data.code == 200) {
            this.reqspecslist();
            this.info.isshow = false;
          }
        });
      }
    },
    opened() {
      if (!this.info.isadd) {
        this.form = this.specsinfo[0];
        this.arr = this.specsinfo[0].attrs.map(item => {
          return { value: item };
        });
      }
    },
    closed() {
      if (!this.info.isadd) {
        this.form = {
          specsname: "",
          attrs: "",
          status: 1
        };
        this.arr = [{value: ""}];
      }
    },
    addArr() {
      this.arr.push({ value: this.arr[0].value });
    },
    del(index) {
      this.arr.splice(index, 1);
    }
  },
  mounted() {}
};
</script>

<style scoped lang="less">
@import "../../../less/index.less";
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.specsbtn {
  display: flex;
  .el-input {
    flex: 1;
  }
}
</style>